package org.geektimes.juc;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * @FileName: JucTest2
 * @Description: TODO
 * @Author: MaWenHao
 * @Created: 2021/3/30 - 上午1:03
 */
@Deprecated
public class JucTest2 {

    public static void main(String[] args) throws InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        for (int i = 0; i < 3; i++) {
            String threadName = "线程" + i;
            new Thread(new MyThread(cyclicBarrier), threadName).start();
        }
        System.out.println("主线程结束...");
    }

    static class MyThread implements Runnable {

        private CyclicBarrier cyclicBarrier;

        public MyThread(CyclicBarrier cyclicBarrier) {
            this.cyclicBarrier = cyclicBarrier;
        }

        @Override
        public void run() {
            Random random = new Random();
            int i = random.nextInt(5) + 1;
            try {
                TimeUnit.SECONDS.sleep(i);
                System.out.printf("线程: %s, 休眠了 %d 秒.... \n", Thread.currentThread().getName(), i);
                cyclicBarrier.await();
                System.out.printf("线程: %s, 结束.... \n", Thread.currentThread().getName());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
        }
    }
}
